export default {
	// 为当前模块开启命名空间
	namespaced: true,

	// 模块的 state 数据
	state: {
		collect: JSON.parse(uni.getStorageSync('collect') || '[]'), //未删除
		deletedCollectId: JSON.parse(uni.getStorageSync('deletedCollect') || '[]'), //已删除
	},

	// 模块的 mutations 方法
	mutations: {
		//添加收藏
		addCollect(state, collectId) {
			state.collect.push(collectId)
			state.collect = [...new Set(state.collect)] //去重
			this.commit('collect/saveCollectToStorage')
		},
		//删除收藏
		deleteCollect(state, collectId) {
			if (typeof collectId == "object") {
				collectId.forEach(itemid => {
					state.deletedCollectId.push(itemid) //批量收集删除过的收藏
					state.collect = state.collect.filter(colsId => colsId != itemid) //没删除，保存
				})
			} else {
				state.deletedCollectId.push(collectId) //收集删除过的收藏
				state.collect = state.collect.filter(itemId => itemId !== collectId) //没删除，保存
			}
			state.deletedCollectId = [...new Set(state.deletedCollectId)] //去重
			this.commit('collect/saveCollectToStorage')
			this.commit('collect/savedeletedCollectToStorage')
		},
		saveCollectToStorage(state) {
			uni.setStorageSync('collect', JSON.stringify(state.collect))
		},
		savedeletedCollectToStorage(state) {
			uni.setStorageSync('deletedCollect', JSON.stringify(state.deletedCollectId))
		}
	},

	// 模块的 getters 属性
	getters: {
		getCollletLenght(state) {
			return state.collect.length
		}
	},
}